package com.itheima.pinda.authority.controller.auth;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.pinda.authority.biz.service.area.AreaService;
import com.itheima.pinda.authority.entity.common.Area;
import com.itheima.pinda.base.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : Lusic
 * @create : 2022/9/5 20:09
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @GetMapping({"/{id}"})
    @ApiOperation(value = "根据ID查询区域信息", notes = "根据ID查询区域信息")
    R<Area> get(@PathVariable Long id){
        if(ObjectUtil.isNotNull(id)){
            QueryWrapper<Area> qw = new QueryWrapper<>();
            qw.eq("area_code",id);
            Area one = this.areaService.getOne(qw);
            return R.success(one);
        }
        return null;
    }

    @GetMapping({"/code/{code}"})
    R<Area> getByCode(@PathVariable String code){
        if(StrUtil.isNotBlank(code)){
            QueryWrapper<Area> qw = new QueryWrapper<>();
            qw.eq("area_code",code);
            Area one = this.areaService.getOne(qw);
            return R.success(one);
        }
        return null;
    }

    @GetMapping
    R<List<Area>> findAll(@RequestParam(value = "parentId",required = false) Long parentId, @RequestParam(value = "ids",required = false) List<Long> ids){
        QueryWrapper<Area> qw = new QueryWrapper<>();
        if(ObjectUtil.isNotNull(parentId)){
            qw.eq("parent_id",parentId);
        }
        if(CollUtil.isNotEmpty(ids)){
            String s = ids.get(0) + "000000";
            if (ids.size()==1){
                qw.eq("area_code",s);
            }else{
                qw.eq("area_code",ids.get(0)).or().eq("area_code",ids.get(1))
                        .or().eq("area_code",ids.get(2));
            }
            List<Area> array = this.areaService.list(qw);
            for (Area arr:array) {
                arr.setId(Long.valueOf(arr.getAreaCode()));
            }
            return R.success(array);
        }else{
            return  R.success(this.areaService.list(qw));
        }
    }
}
